using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VMBEW
    {
        public DataRow Convert2MBEW(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryMBEW(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["MBWMANDANT"] = " ".PadRight(2 ); 
         dwReturn["MBWJJJJ"] = " ".PadRight(0 ); 
         dwReturn["MBWBELEG"] = " ".PadRight(0 ); 
         dwReturn["MBWLFDNR"] = " ".PadRight(0 ); 
         dwReturn["MBWMAT"] = " ".PadRight(14 ); 
         dwReturn["MBWVAR"] = " ".PadRight(3 ); 
         dwReturn["MBWCHAR"] = " ".PadRight(0 ); 
         dwReturn["MBWGNR"] = " ".PadRight(0 ); 
         dwReturn["MBWLO"] = " ".PadRight(2 ); 
         dwReturn["MBWLB"] = " ".PadRight(2 ); 
         dwReturn["MBWLP"] = " ".PadRight(8 ); 
         dwReturn["MBWAUFBKZ"] = " ".PadRight(1 ); 
         dwReturn["MBWVPEINH"] = " ".PadRight(4 ); 
         dwReturn["MBWAUFTYP"] = " ".PadRight(1 ); 
         dwReturn["MBWAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["MBWAUFLFD"] = " ".PadRight(0 ); 
         dwReturn["MBWAUFPOS"] = " ".PadRight(0 ); 
         dwReturn["MBWAFOLGE"] = " ".PadRight(0 ); 
         dwReturn["MBWANSNR"] = " ".PadRight(0 ); 
         dwReturn["MBWRPOS"] = " ".PadRight(0 ); 
         dwReturn["MBWNACHNR"] = " ".PadRight(0 ); 
         dwReturn["MBWMEZU"] = " ".PadRight(0 ); 
         dwReturn["MBWTYP"] = " ".PadRight(1 ); 
         dwReturn["MBWKSTKRS"] = " ".PadRight(2 ); 
         dwReturn["MBWKST"] = " ".PadRight(4 ); 
         dwReturn["MBWMNRKRS"] = " ".PadRight(2 ); 
         dwReturn["MBWMLFDNR"] = " ".PadRight(4 ); 
         dwReturn["MBWMPOS"] = " ".PadRight(2 ); 
         dwReturn["MBWBUDGET"] = " ".PadRight(8 ); 
         dwReturn["MBWBS"] = " ".PadRight(2 ); 
         dwReturn["MBWWERTDAT"] = " ".PadRight(0 ); 
         dwReturn["MBWME"] = " ".PadRight(0 ); 
         dwReturn["MBWWERT"] = " ".PadRight(0 ); 
         dwReturn["MBWWEKORR"] = " ".PadRight(2 ); 
         dwReturn["MBWBUCHBEN"] = " ".PadRight(8 ); 
         dwReturn["MBWBUCHDAT"] = " ".PadRight(0 ); 
         dwReturn["MBWBUCHUHR"] = " ".PadRight(0 ); 
         dwReturn["MBWFC"] = " ".PadRight(4 ); 
         dwReturn["MBWINVBLG"] = " ".PadRight(0 ); 
         dwReturn["MBWCHARL"] = " ".PadRight(15 ); 
         dwReturn["MBWVERBME"] = " ".PadRight(0 ); 
         dwReturn["MBWVERBMEH"] = " ".PadRight(3 ); 
         dwReturn["MBWSTORKZ"] = " ".PadRight(1 ); 
         dwReturn["MBWKONDAT"] = " ".PadRight(0 ); 
         dwReturn["MBWMATABR"] = " ".PadRight(1 ); 
         dwReturn["MBWSCHICHT"] = " ".PadRight(1 ); 
         dwReturn["MBWFILLER"] = " ".PadRight(39 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapMBEW(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveMBEW(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryMBEW(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapMBEW(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertMBEW(dwLocal, ITXDBVERSION);
            }
            UpdateMBEW(dwLocal);
        }
        public void InsertMBEW(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO MBEW(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryMBEW(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM MBEW WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM MBEW  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "MBEW";
            Return = Result;
            return Return;
        }
        public void DeleteMBEW(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM MBEW   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateMBEW(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE MBEW SET "
                    +"MBWMANDANT= :MBWMANDANT,"
                    +"MBWJJJJ= :MBWJJJJ,"
                    +"MBWBELEG= :MBWBELEG,"
                    +"MBWLFDNR= :MBWLFDNR,"
                    +"MBWMAT= :MBWMAT,"
                    +"MBWVAR= :MBWVAR,"
                    +"MBWCHAR= :MBWCHAR,"
                    +"MBWGNR= :MBWGNR,"
                    +"MBWLO= :MBWLO,"
                    +"MBWLB= :MBWLB,"
                    +"MBWLP= :MBWLP,"
                    +"MBWAUFBKZ= :MBWAUFBKZ,"
                    +"MBWVPEINH= :MBWVPEINH,"
                    +"MBWAUFTYP= :MBWAUFTYP,"
                    +"MBWAUFKRS= :MBWAUFKRS,"
                    +"MBWAUFLFD= :MBWAUFLFD,"
                    +"MBWAUFPOS= :MBWAUFPOS,"
                    +"MBWAFOLGE= :MBWAFOLGE,"
                    +"MBWANSNR= :MBWANSNR,"
                    +"MBWRPOS= :MBWRPOS,"
                    +"MBWNACHNR= :MBWNACHNR,"
                    +"MBWMEZU= :MBWMEZU,"
                    +"MBWTYP= :MBWTYP,"
                    +"MBWKSTKRS= :MBWKSTKRS,"
                    +"MBWKST= :MBWKST,"
                    +"MBWMNRKRS= :MBWMNRKRS,"
                    +"MBWMLFDNR= :MBWMLFDNR,"
                    +"MBWMPOS= :MBWMPOS,"
                    +"MBWBUDGET= :MBWBUDGET,"
                    +"MBWBS= :MBWBS,"
                    +"MBWWERTDAT= :MBWWERTDAT,"
                    +"MBWME= :MBWME,"
                    +"MBWWERT= :MBWWERT,"
                    +"MBWWEKORR= :MBWWEKORR,"
                    +"MBWBUCHBEN= :MBWBUCHBEN,"
                    +"MBWBUCHDAT= :MBWBUCHDAT,"
                    +"MBWBUCHUHR= :MBWBUCHUHR,"
                    +"MBWFC= :MBWFC,"
                    +"MBWINVBLG= :MBWINVBLG,"
                    +"MBWCHARL= :MBWCHARL,"
                    +"MBWVERBME= :MBWVERBME,"
                    +"MBWVERBMEH= :MBWVERBMEH,"
                    +"MBWSTORKZ= :MBWSTORKZ,"
                    +"MBWKONDAT= :MBWKONDAT,"
                    +"MBWMATABR= :MBWMATABR,"
                    +"MBWSCHICHT= :MBWSCHICHT,"
                    +"MBWFILLER= :MBWFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("MBWMANDANT", OracleType.NVarChar).Value = dwData["MBWMANDANT"].ToString(); 
          or1.Parameters.Add("MBWJJJJ", OracleType.NVarChar).Value = dwData["MBWJJJJ"].ToString(); 
          or1.Parameters.Add("MBWBELEG", OracleType.NVarChar).Value = dwData["MBWBELEG"].ToString(); 
          or1.Parameters.Add("MBWLFDNR", OracleType.NVarChar).Value = dwData["MBWLFDNR"].ToString(); 
          or1.Parameters.Add("MBWMAT", OracleType.NVarChar).Value = dwData["MBWMAT"].ToString(); 
          or1.Parameters.Add("MBWVAR", OracleType.NVarChar).Value = dwData["MBWVAR"].ToString(); 
          or1.Parameters.Add("MBWCHAR", OracleType.NVarChar).Value = dwData["MBWCHAR"].ToString(); 
          or1.Parameters.Add("MBWGNR", OracleType.NVarChar).Value = dwData["MBWGNR"].ToString(); 
          or1.Parameters.Add("MBWLO", OracleType.NVarChar).Value = dwData["MBWLO"].ToString(); 
          or1.Parameters.Add("MBWLB", OracleType.NVarChar).Value = dwData["MBWLB"].ToString(); 
          or1.Parameters.Add("MBWLP", OracleType.NVarChar).Value = dwData["MBWLP"].ToString(); 
          or1.Parameters.Add("MBWAUFBKZ", OracleType.NVarChar).Value = dwData["MBWAUFBKZ"].ToString(); 
          or1.Parameters.Add("MBWVPEINH", OracleType.NVarChar).Value = dwData["MBWVPEINH"].ToString(); 
          or1.Parameters.Add("MBWAUFTYP", OracleType.NVarChar).Value = dwData["MBWAUFTYP"].ToString(); 
          or1.Parameters.Add("MBWAUFKRS", OracleType.NVarChar).Value = dwData["MBWAUFKRS"].ToString(); 
          or1.Parameters.Add("MBWAUFLFD", OracleType.NVarChar).Value = dwData["MBWAUFLFD"].ToString(); 
          or1.Parameters.Add("MBWAUFPOS", OracleType.NVarChar).Value = dwData["MBWAUFPOS"].ToString(); 
          or1.Parameters.Add("MBWAFOLGE", OracleType.NVarChar).Value = dwData["MBWAFOLGE"].ToString(); 
          or1.Parameters.Add("MBWANSNR", OracleType.NVarChar).Value = dwData["MBWANSNR"].ToString(); 
          or1.Parameters.Add("MBWRPOS", OracleType.NVarChar).Value = dwData["MBWRPOS"].ToString(); 
          or1.Parameters.Add("MBWNACHNR", OracleType.NVarChar).Value = dwData["MBWNACHNR"].ToString(); 
          or1.Parameters.Add("MBWMEZU", OracleType.NVarChar).Value = dwData["MBWMEZU"].ToString(); 
          or1.Parameters.Add("MBWTYP", OracleType.NVarChar).Value = dwData["MBWTYP"].ToString(); 
          or1.Parameters.Add("MBWKSTKRS", OracleType.NVarChar).Value = dwData["MBWKSTKRS"].ToString(); 
          or1.Parameters.Add("MBWKST", OracleType.NVarChar).Value = dwData["MBWKST"].ToString(); 
          or1.Parameters.Add("MBWMNRKRS", OracleType.NVarChar).Value = dwData["MBWMNRKRS"].ToString(); 
          or1.Parameters.Add("MBWMLFDNR", OracleType.NVarChar).Value = dwData["MBWMLFDNR"].ToString(); 
          or1.Parameters.Add("MBWMPOS", OracleType.NVarChar).Value = dwData["MBWMPOS"].ToString(); 
          or1.Parameters.Add("MBWBUDGET", OracleType.NVarChar).Value = dwData["MBWBUDGET"].ToString(); 
          or1.Parameters.Add("MBWBS", OracleType.NVarChar).Value = dwData["MBWBS"].ToString(); 
          or1.Parameters.Add("MBWWERTDAT", OracleType.NVarChar).Value = dwData["MBWWERTDAT"].ToString(); 
          or1.Parameters.Add("MBWME", OracleType.NVarChar).Value = dwData["MBWME"].ToString(); 
          or1.Parameters.Add("MBWWERT", OracleType.NVarChar).Value = dwData["MBWWERT"].ToString(); 
          or1.Parameters.Add("MBWWEKORR", OracleType.NVarChar).Value = dwData["MBWWEKORR"].ToString(); 
          or1.Parameters.Add("MBWBUCHBEN", OracleType.NVarChar).Value = dwData["MBWBUCHBEN"].ToString(); 
          or1.Parameters.Add("MBWBUCHDAT", OracleType.NVarChar).Value = dwData["MBWBUCHDAT"].ToString(); 
          or1.Parameters.Add("MBWBUCHUHR", OracleType.NVarChar).Value = dwData["MBWBUCHUHR"].ToString(); 
          or1.Parameters.Add("MBWFC", OracleType.NVarChar).Value = dwData["MBWFC"].ToString(); 
          or1.Parameters.Add("MBWINVBLG", OracleType.NVarChar).Value = dwData["MBWINVBLG"].ToString(); 
          or1.Parameters.Add("MBWCHARL", OracleType.NVarChar).Value = dwData["MBWCHARL"].ToString(); 
          or1.Parameters.Add("MBWVERBME", OracleType.NVarChar).Value = dwData["MBWVERBME"].ToString(); 
          or1.Parameters.Add("MBWVERBMEH", OracleType.NVarChar).Value = dwData["MBWVERBMEH"].ToString(); 
          or1.Parameters.Add("MBWSTORKZ", OracleType.NVarChar).Value = dwData["MBWSTORKZ"].ToString(); 
          or1.Parameters.Add("MBWKONDAT", OracleType.NVarChar).Value = dwData["MBWKONDAT"].ToString(); 
          or1.Parameters.Add("MBWMATABR", OracleType.NVarChar).Value = dwData["MBWMATABR"].ToString(); 
          or1.Parameters.Add("MBWSCHICHT", OracleType.NVarChar).Value = dwData["MBWSCHICHT"].ToString(); 
          or1.Parameters.Add("MBWFILLER", OracleType.NVarChar).Value = dwData["MBWFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
